package edu.hawaii.senin.iclouds.basics;

import java.text.DecimalFormat;

/**
 * Provides the interval object for the probability calculations. Basic purpose is to check if the
 * value is within the fixed interval or not.
 * 
 * @author Pavel Senin.
 * 
 */
public class Interval {
  /**
   * The name id for this interval.
   */
  private String name;
  /**
   * low bound.
   */
  private Double low;
  /**
   * High bound.
   */
  private Double high;

  private final DecimalFormat FormatDot = new DecimalFormat(".000");

  /**
   * Instantiates new Interval.
   * 
   * @param name the name Id for this interval.
   * @param low low bound.
   * @param high high bound.
   */
  public Interval(String name, Double low, Double high) {
    this.name = name;
    this.low = low;
    this.high = high;
  }

  /**
   * Checks if the provided point is within the interval.
   * 
   * @param value value to check.
   * @return true if the provided value within the interval or false instead.
   */
  public boolean within(Double value) {
    return (this.low <= value) && (value < this.high);
  }

  /**
   * Reports low bound for this interval.
   * 
   * @return low bound.
   */
  public Double getLowBound() {
    return this.low;
  }

  /**
   * Reports high bound for this interval.
   * 
   * @return high bound.
   */
  public Double getHighBound() {
    return this.high;
  }

  /**
   * Prints the formatted interval values into the string separated by comma.
   * 
   * @return the formatted interval.
   */
  public String print() {
    return FormatDot.format(this.low) + "," + FormatDot.format(this.high);
  }

  /**
   * Checks if this interval is equal to given by checking low and high bounds.
   * 
   * @param int2 the interval to compare with.
   * @return true if intervals are equal, false otherwise.
   */
  public boolean equals(Interval int2) {
    if (this.low.equals(int2.getLowBound()) && this.high.equals(int2.getHighBound())) {
      return true;
    }
    return false;
  }

  /**
   * Lame hashcode function.
   * 
   * @return lame hashcode.
   */
  @Override
  public int hashCode() {
    return Double.valueOf(31 * this.low + 17 * this.high).intValue();
  }

  /**
   * Reports the name id.
   * 
   * @return the name id.
   */
  public String getName() {
    return this.name;
  }
}
